package com.itheima.stock.controller;

import com.itheima.stock.mapper.SysRoleMapper;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.pojo.domain.*;
import com.itheima.stock.pojo.entity.SysPermission;
import com.itheima.stock.pojo.entity.SysRole;
import com.itheima.stock.pojo.entity.SysUser;
import com.itheima.stock.pojo.vo.PageResult;
import com.itheima.stock.service.UserService;
import com.itheima.stock.vo.req.LoginReqVo;
import com.itheima.stock.vo.resp.LoginRespVo;
import com.itheima.stock.vo.resp.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api("api")
@RestController
@RequestMapping("api")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "userName", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "GET")
    @GetMapping("/user/{userName}")
    public SysUser test(@PathVariable("userName") String name){
        return userService.findByUserName(name);
    }

    //登录功能
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "LoginReqVo", name = "vo", value = "", required = true)
    })
    @ApiOperation(value = "登录功能", notes = "登录功能", httpMethod = "POST")
    @PostMapping("/login")
    public R<LoginRespVo> login(@RequestBody LoginReqVo vo){
        R<LoginRespVo> l = userService.login(vo);
        return l;
    }

    //获取验证码
    @ApiOperation(value = "获取验证码", notes = "获取验证码", httpMethod = "GET")
    @GetMapping("/captcha")
    public R<Map> getCaptchaCode(){
        return userService.getCaptchaCode();
    }

    /**
     * 多条件综合查询
     * @param findUserMultipleCondition
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "FindUserMultipleCondition", name = "findUserMultipleCondition", value = "", required = true)
    })
    @ApiOperation(value = "多条件综合查询", notes = "多条件综合查询", httpMethod = "POST")
    @PostMapping("/users")
    public R<PageResult> getUserList(@RequestBody FindUserMultipleCondition findUserMultipleCondition){
        return userService.getUserList(findUserMultipleCondition);
    }

    /**
     * 添加用户
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "UserInfoToAdd", name = "userInfoToAdd", value = "", required = true)
    })
    @ApiOperation(value = "添加用户", notes = "添加用户", httpMethod = "POST")
    @PostMapping("/user")
        public R AddUser(@RequestBody UserInfoToAdd userInfoToAdd){
            return userService.AddUser(userInfoToAdd);
        }

    /**
     *  获取用户具有的角色信息接口
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "userId", value = "", required = true)
    })
    @ApiOperation(value = "获取用户具有的角色信息接口", notes = "获取用户具有的角色信息接口", httpMethod = "GET")
    @GetMapping("/user/roles/{userId}")
    public R<Map<String, List>> getUserRoleInfo(@PathVariable String userId){
        return userService.getUserRoleInfo(userId);
    }

    /**
     * 用户添加角色
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "UserAddRole", name = "userAddRole", value = "", required = true)
    })
    @ApiOperation(value = "用户添加角色", notes = "用户添加角色", httpMethod = "PUT")
    @PutMapping("/user/roles")
    public R addUserRole(@RequestBody UserAddRole userAddRole){
        return userService.addUserRole(userAddRole);
    }

    /**
     * 删除用户信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "List<Long>", name = "userIds", value = "", required = true)
    })
    @ApiOperation(value = "删除用户信息", notes = "删除用户信息", httpMethod = "DELETE")
    @DeleteMapping("/user")
    public R deleteUserInfo(@RequestBody List<Long> userIds){
        return userService.deleteUserInfo(userIds);
    }

    /**
     * 根据id查询用户信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "userId", value = "", required = true)
    })
    @ApiOperation(value = "根据id查询用户信息", notes = "根据id查询用户信息", httpMethod = "GET")
    @GetMapping("/user/info/{userId}")
    public R<SysInfoById> getUserInfoById(@PathVariable Long userId){
        return userService.getUserInfoById(userId);
    }

    /**
     * 根据id更新用户基本信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysInfoByIdToUpDate", name = "sysInfoByIdToUpDate", value = "", required = true)
    })
    @ApiOperation(value = "根据id更新用户基本信息", notes = "根据id更新用户基本信息", httpMethod = "PUT")
    @PutMapping("/user")
    public R updateUserInfoById(@RequestBody SysInfoByIdToUpDate sysInfoByIdToUpDate){
        return userService.updateUserInfoById(sysInfoByIdToUpDate);
    }

    /**
     * 分页查询当前角色信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "FenyeForRole", name = "condition", value = "", required = true)
    })
    @ApiOperation(value = "分页查询当前角色信息", notes = "分页查询当前角色信息", httpMethod = "POST")
    @PostMapping("/roles")
    public R<PageResult> getUserRoleList(@RequestBody FenyeForRole condition){
        return userService.getUserRoleList(condition);
    }

    /**
     * 根据id删除角色
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "roleId", value = "", required = true)
    })
    @ApiOperation(value = "根据id删除角色", notes = "根据id删除角色", httpMethod = "DELETE")
    @DeleteMapping("/role/{roleId}")
    public R deleteRoleById(@PathVariable Long roleId){
        return userService.deleteRoleById(roleId);
    }

    /**
     * 更新角色的状态
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "roleId", value = "", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "status", value = "", required = true)
    })
    @ApiOperation(value = "更新角色的状态", notes = "更新角色的状态", httpMethod = "POST")
    @PostMapping("/role/{roleId}/{status}")
    public R updateUserStatus(@PathVariable("roleId") Long roleId, @PathVariable("status") Integer status){
        return userService.updateUserStatus(roleId, status);
    }

    /**
     * 权限列表展示功能
     */
    @ApiOperation(value = "权限列表展示功能", notes = "权限列表展示功能", httpMethod = "GET")
    @GetMapping("/permissions")
    public R<List<SysPermission>> getPermissionList(){
        return userService.getPermissionList();
    }

    /**
     *添加权限时回显权限树
     */
    @ApiOperation(value = "添加权限时回显权限树", notes = "添加权限时回显权限树", httpMethod = "GET")
    @GetMapping("/permissions/tree")
    public R<List<PermissionTree>> getPermissionTree(){
        return userService.getPermissionTree();
    }

    /**
     * 权限添加按钮
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "PermissionForAdd", name = "permissionForAdd", value = "", required = true)
    })
    @ApiOperation(value = "权限添加按钮", notes = "权限添加按钮", httpMethod = "POST")
    @PostMapping("/permission")
    public R<PermissionForAdd> addPermission(@RequestBody PermissionForAdd permissionForAdd){
        return userService.addPermission(permissionForAdd);
    }

    /**
     * 更新权限
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "PermissionForUpdate", name = "permissionForUpdate", value = "", required = true)
    })
    @ApiOperation(value = "更新权限", notes = "更新权限", httpMethod = "PUT")
    @PutMapping("/permission")
    public R updatePermission(@RequestBody PermissionForUpdate permissionForUpdate){
        return userService.updatePermission(permissionForUpdate);
    }

    /**
     * 删除权限
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "permissionId", value = "", required = true)
    })
    @ApiOperation(value = "删除权限", notes = "删除权限", httpMethod = "DELETE")
    @DeleteMapping("/permission/{permissionId}")
    public R deletePermission(@PathVariable Long permissionId){
        return userService.deletePermission(permissionId);
    }
}
